﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using System.Threading;
using System.Windows.Data;
using System.ServiceModel;
using Sl.OperatorUnitManagement.CommonFunctionList;

namespace Sl.OperatorUnitManagement.CompetenceSetPaeg.AccountManagement
{
    public partial class AccountManagementListShowChild : ChildWindow
    {
         App AppConfig = (App)Application.Current;
        //消息子窗口
        CommonFunctionList.MessageBox Messbox = new CommonFunctionList.MessageBox();

        public AccountManagementListShowChild()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 确定按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
            lbList.Items.Clear();
        }

        /// <summary>
        /// 取消按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            lbList.Items.Clear();
        }

        #region 搜索框KeyDown事件

        /// <summary>
        /// 搜索框KeyDown事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tbInput_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.Key == Key.Enter)
                {
                    WCFDecomposition.AccountsRef.WCFAccountsClient _client = new Sl.OperatorUnitManagement.WCFDecomposition.AccountsRef.WCFAccountsClient();
                    _client.AccountManagementAllCompleted += new EventHandler<Sl.OperatorUnitManagement.WCFDecomposition.AccountsRef.AccountManagementAllCompletedEventArgs>(client_AccountManagementAll);
                    
                    if (string.IsNullOrEmpty(this.tbInput.Text))
                    {
                        _client.AccountManagementAllAsync("%");
                    }
                    else
                    {
                        _client.AccountManagementAllAsync(this.tbInput.Text.Trim());
                    }
                }
            }
            catch (Exception ex) 
            {
                  CommonFunctionList.FunctionClass.ShowMessageBox(
                    Messbox,"提示",
                    ex.Message.ToString(),
                    Colors.Gray, 
                    true,
                    Colors.Red,
                    Visibility.Visible,
                    Visibility.Collapsed);
            }
        }
        #endregion 搜索框KeyDown事件


        #region  单击搜索框，向DATAGRID中添加信息
        /// <summary>
        /// 单击搜索框，向DATAGRID中添加信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void client_AccountManagementAll
                (object sender, WCFDecomposition.AccountsRef.AccountManagementAllCompletedEventArgs e) 
        {
            try
            {
                if (e.Error == null)
                {
                    if (e.Result.Count > 0)
                    {
                        //分页控件
                        PagedCollectionView pcv = new PagedCollectionView(e.Result);
                        pcv.PageSize = 9;

                        this.dgAMList.DataContext  = pcv;
                        this.dpDataPage.DataContext = pcv;
                    }
                    else
                    {
                        throw (new Exception("没有可显示的数据"));
                    }
                }
                else
                {
                    throw (new Exception(e.Error.Message.ToString()));
                }
            }
            catch (Exception ex) 
            {
                CommonFunctionList.FunctionClass.ShowMessageBox(
                    Messbox, "提示", 
                    ex.Message.ToString(),
                    Colors.Gray, 
                    true, 
                    Colors.Red,
                    Visibility.Visible, 
                    Visibility.Collapsed);
            }

        }
        #endregion 单击搜索框，向DATAGRID中添加信息


        #region 点击按钮加入列表
        /// <summary>
        /// DataGrid上的按钮
        /// 点击按钮加入列表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAddToList_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string accountID = ((Button)sender).Tag.ToString();
                bool bAddAccount = false;

                if (ClientDataList.ParameterAccountList.Count != 0)
                {
                    int index = ClientDataList.ParameterAccountList.IndexOf(accountID);
                    if (index == -1)
                    {
                        bAddAccount = true;
                    }
                }
                else
                {
                    bAddAccount = true;
                }

                if (bAddAccount)
                {
                    ClientDataList.ParameterAccountList.Add(accountID.Trim());
                    lbList.Items.Add(((Button)sender).Tag.ToString());

                }

            }
            catch (Exception ex)
            {
                CommonFunctionList.FunctionClass.ShowMessageBox(
                    Messbox, "提示",
                    ex.Message.ToString(),
                    Colors.Gray,
                    true,
                    Colors.Red,
                    Visibility.Visible,
                    Visibility.Collapsed);
            }

        }

        #endregion 点击按钮加入列表


        #region 事件绑定的实现 无效
        ///// <summary>
        ///// datagrid 事件绑定的实现
        ///// </summary>
        ///// <param name="sender"></param>
        ///// <param name="e"></param>
        //private void dgAMList_SelectionChanged(object sender, EventArgs e)
        //{


        //    //DataGrid grid = sender as DataGrid;
        //    //WCFDecomposition.AccountsRef.AccountManagementDataList book = (WCFDecomposition.AccountsRef.AccountManagementDataList)grid.SelectedItems[0];//获取当前选中的行数据并转换为对应的实体
        //    //MessageBox.Show(book.AccountID.ToString());      //打印该实体对应的属性值
        //    /*Customer obj = (sender as ListBox).SelectedItem as Customer;
        //     MessageBox.Show(obj.FirstName.ToString());
        //     我相信你也定了一个Class吧。Customer 就是我定义的class，FirstName就是我选中列的字段名。
        //   */
        //    WCFDecomposition.AccountsRef.AccountManagementDataList amdl = (WCFDecomposition.AccountsRef.AccountManagementDataList)this.dgAMList.SelectedItem;
        //    MessageBox.Show(amdl.AccountID.ToString());

        //}
        #endregion 事件绑定的实现 无效
    

    }
}

